package com.example.classloader;

import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Driver;
import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * @author shanchong
 * @date 2021/2/25
 **/
public class Main {

    public static void main(String[] args) throws ClassNotFoundException, MalformedURLException {
        ParentClassLoader parentClassLoader = new ParentClassLoader();
        URL url = new URL("");
        MyClassLoader myClassLoader = new MyClassLoader(new URL[]{url}, parentClassLoader);
        myClassLoader.loadClass("com.mysql.cj.jdbc.Driver");
        Thread.currentThread().setContextClassLoader(myClassLoader);
        ServiceLoader<Driver> serviceLoader = ServiceLoader.load(Driver.class);
//        ServiceLoader<Driver> serviceLoader = ServiceLoader.load(Driver.class, myClassLoader);
        Iterator<Driver> iterator = serviceLoader.iterator();
        while (iterator.hasNext()) {
            Class<? extends Driver> aClass = iterator.next().getClass();
            System.out.println(aClass + "===" + aClass.getClassLoader());
        }
    }

}
